﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model.WXshopBuiness;
using Xproject.Framework.Core.Repositories;
using Xproject.Framework.Utility.Extensions;

namespace Repository.WXshopBuiness
{
   public static class WXProductRep
    {
        public static List<WXProduct> GetAllWXProducts(this IRepositoryAsync<WXProduct> repository, string site,  string productName, string productCode, string productDesc, string key,
            int? isSheepFree, string remarks, int? state, DateTime? startTime, DateTime? endTime, string order, string ordername)
        {
            Expression<Func<WXProduct, bool>> express = i => i.Site ==site;
            if (!string.IsNullOrEmpty(productName))
            {
                express = express.AndAlso(i => i.ProductName.Contains(productName));
            }
            if (!string.IsNullOrEmpty(productCode))
            {
                express = express.AndAlso(i => i.ProductCode.Contains(productCode));
            }
            if (!string.IsNullOrEmpty(productDesc))
            {
                express = express.AndAlso(i => i.DetailDesc.Contains(productDesc));
            }
            if (!string.IsNullOrEmpty(key))
            {
                express = express.AndAlso(i => i.ShortDesc.Contains(key));
            }
            if (isSheepFree.HasValue)
            {
                express = express.AndAlso(i => i.IsFreeShipping==isSheepFree.Value);
            }
            if (state.HasValue)
            {
                express = express.AndAlso(i => i.State == state.Value);
            }
            if (!string.IsNullOrEmpty(remarks))
            {
                express = express.AndAlso(i => i.Remark.Contains(remarks));
            }
            
            if (startTime.HasValue)
            {
                express = express.AndAlso(i => i.CreateTime >= startTime.Value);
            }
            if (endTime.HasValue)
            {
                express = express.AndAlso(i => i.CreateTime <= endTime.Value);
            }
            return repository.Query().Where(express).Select().OrderByDescending(i => i.CreateTime).ToList();
        }

        public static WXProduct Get(this IRepositoryAsync<WXProduct> repository, string productId)
        {
            Expression<Func<WXProduct, bool>> express = i => i.ProductID == productId;
            return repository.Query().Where(express).Select().FirstOrDefault();
        }

        public static WXProduct GetByProductCode(this IRepositoryAsync<WXProduct> repository, string productCode)
        {
            Expression<Func<WXProduct, bool>> express = i => i.ProductCode == productCode;
            return repository.Query().Where(express).Select().FirstOrDefault();
        }



        public static List<WXProduct> GetAllWXProducts(this IRepositoryAsync<WXProduct> repository, string site)
        {
            Expression<Func<WXProduct, bool>> express = i => i.Site == site;
            return repository.Query().Where(express).Select().OrderByDescending(i=>i.SortID).ToList();
        }
    }
}
